package com.zf.yichat.service.impl;


import com.zf.yichat.mapper.LotteryPrizeMapper;
import com.zf.yichat.mapper.UserLotteryPrizeMapper;
import com.zf.yichat.model.LotteryPrize;
import com.zf.yichat.model.UserLotteryPrize;
import com.zf.yichat.service.LotteryService;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.common.FsConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class LotteryServiceImpl implements LotteryService {

    @Autowired
    private LotteryPrizeMapper lotteryPrizeMapper;
    @Autowired
    private UserLotteryPrizeMapper userLotteryPrizeMapper;

    @Override
    public List<LotteryPrize> queryPrizeList() {
        Example example = new Example(LotteryPrize.class);
        example.createCriteria().andEqualTo("state", FsConst.Status.EFFECT);
        return DtoChangeUtils.getList(lotteryPrizeMapper.selectByExample(example), prize -> {
            LotteryPrize dto = new LotteryPrize();
            BeanUtils.copyProperties(prize, dto);
            return dto;
        });
    }

    @Override
    public void addUserLotteryPrize(Long userId, LotteryPrize prize) {

        UserLotteryPrize userLotteryPrize = new UserLotteryPrize();
        userLotteryPrize.setPrizeId(prize.getId());
        userLotteryPrize.setUserId(userId);
        userLotteryPrize.setPrizeName(prize.getPrizeName());
        userLotteryPrize.setPrizePic(prize.getPrizePic());
        userLotteryPrize.setCreateTime(new Date());
        userLotteryPrize.setStatus(UserLotteryPrize.STATE_0);
        userLotteryPrizeMapper.insert(userLotteryPrize);
    }

    @Override
    public List<UserLotteryPrize> queryLotteryList(Long userId) {

        Example example = new Example(UserLotteryPrize.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        example.setOrderByClause("create_time desc");
        return userLotteryPrizeMapper.selectByExample(example);
    }
}
